<template>
  <div class="order-root">
    <el-card class="order-card">
      <div class="order-header">
        <h2>订单管理</h2>
        <el-input
          v-model="search"
          placeholder="搜索用户/商家"
          clearable
          class="order-search"
          @input="filterOrders"
        />
        <el-select
          v-model="statusFilter"
          placeholder="全部状态"
          clearable
          class="order-select"
          @change="filterOrders"
        >
          <el-option
            label="全部状态"
            value=""
          />
          <el-option
            label="已完成"
            value="已完成"
          />
          <el-option
            label="配送中"
            value="配送中"
          />
          <el-option
            label="已取消"
            value="已取消"
          />
        </el-select>
        <el-button
          type="primary"
          class="order-btn"
          @click="exportOrders"
        >
          导出订单
        </el-button>
        <el-button
          type="danger"
          :disabled="!multipleSelection.length"
          class="order-btn"
          @click="batchDelete"
        >
          批量删除
        </el-button>
      </div>
      <el-table
        :data="pagedOrders"
        style="width: 100%; margin-top: 16px;"
        border
        @row-click="handleRowClick"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="48"
        />
        <el-table-column
          prop="id"
          label="订单号"
          width="100"
        />
        <el-table-column
          prop="user"
          label="用户"
        />
        <el-table-column
          prop="shop"
          label="商家"
        />
        <el-table-column
          prop="amount"
          label="金额"
        />
        <el-table-column
          label="状态"
          width="120"
        >
          <template #default="scope">
            <el-tag
              :type="scope.row.status === '已完成' ? 'success' : (scope.row.status === '配送中' ? 'warning' : 'info')"
              style="cursor:pointer;"
              @click.stop="toggleStatus(scope.row)"
            >
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="220"
        >
          <template #default="scope">
            <el-button
              size="small"
              @click.stop="showDetail(scope.row)"
            >
              详情
            </el-button>
            <el-button
              size="small"
              type="primary"
              @click.stop="editOrder(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click.stop="deleteOrder(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="currentPage"
        class="order-pagination"
        background
        layout="prev, pager, next"
        :total="filteredOrders.length"
        :page-size="pageSize"
      />
    </el-card>
    <el-dialog
      v-model="dialogVisible"
      :title="dialogMode === 'edit' ? '编辑订单' : '订单详情'"
      width="420px"
      class="order-dialog"
      :close-on-click-modal="false"
    >
      <el-card
        v-if="currentOrder"
        class="order-detail-card"
      >
        <el-form
          v-if="dialogMode === 'edit'"
          :model="currentOrder"
          label-width="80px"
        >
          <el-form-item label="用户">
            <el-input v-model="currentOrder.user" />
          </el-form-item>
          <el-form-item label="商家">
            <el-input v-model="currentOrder.shop" />
          </el-form-item>
          <el-form-item label="金额">
            <el-input-number
              v-model="currentOrder.amount"
              :min="0"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="currentOrder.status">
              <el-option
                label="已完成"
                value="已完成"
              />
              <el-option
                label="配送中"
                value="配送中"
              />
              <el-option
                label="已取消"
                value="已取消"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <el-descriptions
          v-else
          :column="1"
          border
        >
          <el-descriptions-item label="用户">
            {{ currentOrder.user }}
          </el-descriptions-item>
          <el-descriptions-item label="商家">
            {{ currentOrder.shop }}
          </el-descriptions-item>
          <el-descriptions-item label="金额">
            ￥{{ currentOrder.amount }}
          </el-descriptions-item>
          <el-descriptions-item label="状态">
            {{ currentOrder.status }}
          </el-descriptions-item>
        </el-descriptions>
      </el-card>
      <template #footer>
        <el-button
          v-if="dialogMode === 'edit'"
          type="primary"
          @click="saveOrder"
        >
          保存
        </el-button>
        <el-button @click="dialogVisible = false">
          关闭
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

interface Order {
  id: string
  user: string
  shop: string
  amount: number
  status: string
}

const orders = ref<Order[]>([
  { id: '1001', user: '张三', shop: '食堂一号店', amount: 25, status: '已完成' },
  { id: '1002', user: '李四', shop: '校园奶茶', amount: 18, status: '配送中' },
  { id: '1003', user: '王五', shop: '炸鸡汉堡', amount: 32, status: '已取消' },
  { id: '1004', user: '赵六', shop: '健康轻食', amount: 22, status: '已完成' },
  { id: '1005', user: '钱七', shop: '食堂一号店', amount: 19, status: '配送中' },
  { id: '1006', user: '孙八', shop: '校园奶茶', amount: 27, status: '已完成' }
])

const search = ref('')
const statusFilter = ref('')
const filteredOrders = ref<Order[]>([...orders.value])

const filterOrders = () => {
  filteredOrders.value = orders.value.filter(order => {
    const matchSearch =
      !search.value ||
      order.user.includes(search.value) ||
      order.shop.includes(search.value)
    const matchStatus = !statusFilter.value || order.status === statusFilter.value
    return matchSearch && matchStatus
  })
  currentPage.value = 1
}

const pageSize = 4
const currentPage = ref(1)
const pagedOrders = computed(() => {
  const start = (currentPage.value - 1) * pageSize
  return filteredOrders.value.slice(start, start + pageSize)
})

// 多选相关
const multipleSelection = ref<Order[]>([])
const handleSelectionChange = (val: Order[]) => {
  multipleSelection.value = val
}
const batchDelete = () => {
  ElMessageBox.confirm('确定要删除选中的订单吗？', '提示', { type: 'warning' })
    .then(() => {
      orders.value = orders.value.filter(order => !multipleSelection.value.includes(order))
      filterOrders()
      ElMessage.success('批量删除成功')
    })
}

// 行操作相关
const dialogVisible = ref(false)
const currentOrder = ref<Order | null>(null)
const dialogMode = ref<'detail' | 'edit'>('detail')

const showDetail = (row: Order) => {
  currentOrder.value = { ...row }
  dialogMode.value = 'detail'
  dialogVisible.value = true
}
const editOrder = (row: Order) => {
  currentOrder.value = { ...row }
  dialogMode.value = 'edit'
  dialogVisible.value = true
}
const saveOrder = () => {
  if (!currentOrder.value) return
  const idx = orders.value.findIndex(o => o.id === currentOrder.value!.id)
  if (idx !== -1) orders.value[idx] = { ...currentOrder.value }
  filterOrders()
  dialogVisible.value = false
  ElMessage.success('订单已保存')
}
const deleteOrder = (row: Order) => {
  ElMessageBox.confirm('确定要删除该订单吗？', '提示', { type: 'warning' })
    .then(() => {
      orders.value = orders.value.filter(order => order.id !== row.id)
      filterOrders()
      ElMessage.success('删除成功')
    })
}

// 状态切换
const toggleStatus = (row: Order) => {
  if (row.status === '配送中') row.status = '已完成'
  else if (row.status === '已完成') row.status = '已取消'
  else row.status = '配送中'
  filterOrders()
  ElMessage.info('状态已切换为：' + row.status)
}

// 详情弹窗
const handleRowClick = (row: Order) => {
  showDetail(row)
}

// 导出订单（模拟）
const exportOrders = () => {
  ElMessage.success('导出成功（模拟）')
}
</script>

<style scoped>
.order-root {
  max-width: 900px;
  margin: 0 auto;
  padding: 32px 0 40px 0;
}
.order-card {
  border-radius: 14px;
  box-shadow: 0 2px 12px #e6e6e6;
  padding: 24px 18px 32px 18px;
}
.order-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 8px;
}
.order-search {
  width: 220px;
}
.order-select {
  width: 140px;
}
.order-btn {
  margin-left: 8px;
}
.order-pagination {
  margin-top: 18px;
  text-align: right;
}
.order-dialog .el-dialog__body {
  padding: 0 24px 24px 24px;
}
.order-detail-card {
  border-radius: 10px;
  box-shadow: 0 2px 8px #f0f1f2;
  padding: 18px 12px 12px 12px;
  background: #f9fbff;
}
.order-detail-title {
  font-size: 1.1rem;
  font-weight: 500;
  margin-bottom: 12px;
  color: #409EFF;
}
</style> 